/*
 * @lc app=leetcode.cn id=19 lang=golang
 *
 * [19] 删除链表的倒数第 N 个结点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func removeNthFromEnd(head *ListNode, n int) *ListNode {
	pre := &ListNode{Val: -1, Next: head}
	ans := pre
	ptr := pre
	length := 0
	for ptr != nil {
		ptr = ptr.Next
		length++
	}
	for length > n+1 {
		pre = pre.Next
		length--
	}
	pre.Next = pre.Next.Next

	return ans.Next
}

// @lc code=end

